/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tletlis.UI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import tletlis.controller.Controller;
import tletlis.controller.Update;
import tletlis.network.MyIp;


/**
 *
 * @author OLBZINATOR^^^MTHfCKr
 *
 * The multiplayer class creates a JFrame from which the user can either
 * host a tletlis game or join a tletlis game (if there's a current game).
 * 
 */
public class Multiplayer extends JFrame implements ActionListener {
        
    JButton hostButton = new JButton("Host");
    JButton joinButton = new JButton("Join");
    JButton backButton = new JButton("Back");

    String playerName;
    String playerIP;
    boolean isServer;
    JPanel multPanel = new JPanel();
    

    public Multiplayer(){

        setPreferredSize(new Dimension(400,200));
       
        
        multPanel.setLayout(new GridLayout(3,1));

        hostButton.setPreferredSize(new Dimension(25,50));
        joinButton.setPreferredSize(new Dimension(25,50));
        backButton.setPreferredSize(new Dimension(15,50));
        hostButton.addActionListener(this);
        joinButton.addActionListener(this);
        backButton.addActionListener(this);
        
        multPanel.setPreferredSize(new Dimension(65,150));
        multPanel.add(hostButton);
        multPanel.add(joinButton);
        multPanel.add(backButton);


        multPanel.setOpaque(true);
        multPanel.setVisible(true);
        multPanel.setBackground(Color.BLACK);
        
        add(multPanel);
        this.setTitle("Tletlis ©");


        setVisible(true);
        pack();

        
    }

    /**
     * Gets an event which can either be a mouseclick on the host-button or
     * the join-button, and then starts the game.
     *
     * If it is the host-button you will have to enter your name.
     *
     * If it is the join-button you will have to enter your opponents IP-adress
     * and your name.
     *
     * @param e - the action
     */
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(backButton)){
            JFrame welcome = new WelcomeScreen();
            welcome.setSize(500, 625);
            welcome.setVisible(true);
            this.dispose();
        }
        else {
            playerName = JOptionPane.showInputDialog(null,"Mata in ditt namn: ");
            if(playerName.equals(""))
                playerName="Player";
            if(e.getSource().equals(hostButton)){
                isServer = true;
            }
            else if(e.getSource().equals(joinButton)){
                playerIP = JOptionPane.showInputDialog(null,"Mata in motståndarens IP-adress: ");
                isServer = false;
            }
            new Update(2, isServer, playerIP, playerName);
            this.setVisible(false);
        }
    }
}
